package com.deuwise.system.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.deuwise.common.exception.RException;
import com.deuwise.common.utils.Constant;
import com.deuwise.common.utils.cache.Cache;
import com.deuwise.system.entity.SysRole;
import com.deuwise.system.service.SysRoleMenuService;
import com.deuwise.system.service.SysRoleService;
import com.deuwise.system.mapper.SysRoleMapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.deuwise.system.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户接口 接口实现类
 *@author deuwise
 *@date 2018-11-06 14:15:04
 **/
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    Logger logger= LoggerFactory.getLogger(SysRoleServiceImpl.class);
    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Autowired
    private SysUserService sysUserService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SysRole role) {

        baseMapper.insert(role);
        //检查权限是否越权预留以后使用       // checkPrems(role);

        //保存角色与菜单关系
        sysRoleMenuService.saveOrUpdate(role.getId(), role.getMenuIdList(),role.getSystemId());
    }


    @Override
    public Integer queryByName(String name, SysRole sysRole) {
        Wrapper<SysRole> dictDoEntityWrapper=new EntityWrapper<>();
        dictDoEntityWrapper.eq("role_name",name);
        dictDoEntityWrapper.ne("invented",1);
        if(sysRole!=null){
            if(StringUtils.isNotBlank(sysRole.getId())) {
                dictDoEntityWrapper.ne("id", sysRole.getId());
            }
        }
        return baseMapper.selectCount(dictDoEntityWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysRole role) {
        try{
            baseMapper.updateById(role);
        }catch (Exception ex){
            ex.printStackTrace();
            logger.error("角色修改异常--->>>" + ex.getMessage());
        }
        //检查权限是否越权预留以后使用       // checkPrems(role);

        try{
            //保存角色与菜单关系
            sysRoleMenuService.saveOrUpdate(role.getId(), role.getMenuIdList(),role.getSystemId());
            //删除角色菜单缓存
            Cache.removeLike("permsSet");
        }catch (Exception ex){
            ex.printStackTrace();
            logger.error("角色与菜单关系保存异常--->>>" + ex.getMessage());
        }
    }


    /**
     * 检查权限是否越权
     */
    private void checkPrems(SysRole role){
        //如果不是超级管理员，则需要判断角色的权限是否超过自己的权限
        if(role.getCreateAt().equals(Constant.SUPER_ADMIN)){
            return ;
        }

        //查询用户所拥有的菜单列表
        List<String> menuIdList = sysUserService.queryAllMenuId(role.getCreateAt(),"dwAdmin");

        //判断是否越权
        if(!menuIdList.containsAll(role.getMenuIdList())){
            throw new RException("新增角色的权限，已超出你的权限范围");
        }
    }
}